package com.ego.dubbo.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.utils.IDUtils;
import com.ego.dubbo.service.TBItemDubboService;
import com.ego.mapper.TbItemDescMapper;
import com.ego.mapper.TbItemMapper;
import com.ego.mapper.TbItemParamItemMapper;
import com.ego.pojo.TbItem;
import com.ego.pojo.TbItemDesc;
import com.ego.pojo.TbItemExample;
import com.ego.pojo.TbItemParamItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@DubboService
public class TBItemDubboServiceImpl  implements TBItemDubboService {
    
    
    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbItemDescMapper tbItemDescMapper;


    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;
    
    @Override
    public PageInfo<TbItem> selectTbItemByPage(int page, int size) {

        //开始分页: 注意分页一定要查询之前执行
        PageHelper.startPage(page,size);

        
        //创建条件对象
        //TbItemExample tbItemExample = new TbItemExample();

        List<TbItem> tbItems = tbItemMapper.selectByExample(null);




        //创建分页对象
        PageInfo<TbItem> tbItemPageInfo = new PageInfo<>(tbItems);


        return tbItemPageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateItemStatusByIds(long[] ids, int status) throws DaoException {
        
        int index = 0;

        Date updateTime = new Date();
        for (long id : ids) {

            TbItem tbItem = new TbItem();
            tbItem.setId(id);
            tbItem.setStatus((byte)status);
            //设置修改时间
            tbItem.setUpdated(updateTime);
            index +=tbItemMapper.updateByPrimaryKeySelective(tbItem);
            //System.out.println(1/0);
        }

        if(index == ids.length){
            return 1;
        }

        throw new DaoException("修改状态失败，请联系管理员");
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(TbItem tbItem, TbItemDesc tbItemDesc, TbItemParamItem tbItemParamItem) throws DaoException {

        int index = 0;

        try {
            Date now = new Date();

            //设置创建时间和更新时间
            tbItem.setCreated(now);
            tbItem.setUpdated(now);

            //设置状态默认为上架状态
            tbItem.setStatus((byte)1);
            //新增商品
            index = tbItemMapper.insert(tbItem);
            if(index == 1){

                //设置商品id
                tbItemDesc.setItemId(tbItem.getId());
                tbItemDesc.setCreated(now);
                tbItemDesc.setUpdated(now);

                //新增商品描述
                index = tbItemDescMapper.insert(tbItemDesc);



                if(index == 1){
                    //设置商品id
                    tbItemParamItem.setItemId(tbItem.getId());
                    //设置规格参数的创建时间和更新时间
                    tbItemParamItem.setCreated(now);
                    tbItemParamItem.setUpdated(now);

                    index = tbItemParamItemMapper.insert(tbItemParamItem);

                }
            }

            if(index == 1){
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new DaoException("新增商品失败，请联系管理员");
        }
        throw new DaoException("新增商品失败，请联系管理员");
    }

    @Transactional(rollbackFor = Exception.class)
    public int update(TbItem tbItem, TbItemDesc tbItemDesc,TbItemParamItem tbItemParamItem) throws DaoException {

        int index = 0;

        Date now = new Date();
        //设置商品修改时间
        tbItem.setUpdated(now);
        //设置商品描述的修改时间
        tbItemDesc.setUpdated(now);
        //设置商品规格参数的修改时间
        tbItemParamItem.setUpdated(now);

        //设置商品描述要修改id
        tbItemDesc.setItemId(tbItem.getId());

        try {
            //修改商品
            index = tbItemMapper.updateByPrimaryKeySelective(tbItem);

            if(index == 1){
                //修改商品描述
                index = tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);

                if(index == 1){
                    //修改商品规格参数
                  index =  tbItemParamItemMapper.updateByPrimaryKeySelective(tbItemParamItem);
                }


                if(index == 1){
                    return index;
                }
            }
        }  catch (Exception e) {
            e.printStackTrace();
            throw new DaoException("修改商品失败，请联系管理员");
        }
        throw new DaoException("修改商品失败，请联系管理员");
    }

    @Override
    public List<TbItem> selectByIds(List<Long> ids) {

        TbItemExample tbItemExample = new TbItemExample();
        tbItemExample.createCriteria().andIdIn(ids);

        List<TbItem> tbItems = tbItemMapper.selectByExample(tbItemExample);

        return tbItems;
    }

    @Override
    public TbItem selectById(Long id) {
        return tbItemMapper.selectByPrimaryKey(id);
    }
}
